package com.cindata

import grails.transaction.Transactional

import static org.springframework.http.HttpStatus.*

@Transactional
class AccountsReceivableImportBatchController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond AccountsReceivableImportBatch.list(params), model: [accountsReceivableImportBatchCount: AccountsReceivableImportBatch.count()]
    }

    def show(AccountsReceivableImportBatch accountsReceivableImportBatch)
    {
        respond accountsReceivableImportBatch
    }

    def create()
    {
        respond new AccountsReceivableImportBatch(params)
    }

    @Transactional
    def save(AccountsReceivableImportBatch accountsReceivableImportBatch)
    {
        if (accountsReceivableImportBatch == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountsReceivableImportBatch.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountsReceivableImportBatch.errors, view: 'create'
            return
        }

        accountsReceivableImportBatch.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'accountsReceivableImportBatch.label', default: 'AccountsReceivableImportBatch'), accountsReceivableImportBatch.id])
                redirect accountsReceivableImportBatch
            }
            '*' { respond accountsReceivableImportBatch, [status: CREATED] }
        }
    }

    def edit(AccountsReceivableImportBatch accountsReceivableImportBatch)
    {
        respond accountsReceivableImportBatch
    }

    @Transactional
    def update(AccountsReceivableImportBatch accountsReceivableImportBatch)
    {
        if (accountsReceivableImportBatch == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountsReceivableImportBatch.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond accountsReceivableImportBatch.errors, view: 'edit'
            return
        }

        accountsReceivableImportBatch.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountsReceivableImportBatch.label', default: 'AccountsReceivableImportBatch'), accountsReceivableImportBatch.id])
                redirect accountsReceivableImportBatch
            }
            '*' { respond accountsReceivableImportBatch, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountsReceivableImportBatch accountsReceivableImportBatch)
    {

        if (accountsReceivableImportBatch == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountsReceivableImportBatch.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountsReceivableImportBatch.label', default: 'AccountsReceivableImportBatch'), accountsReceivableImportBatch.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountsReceivableImportBatch.label', default: 'AccountsReceivableImportBatch'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
